package logic

import (
	"context"
	"goods_srv/goodssrv"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddOrderLogic {
	return &AddOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddOrderLogic) AddOrder(req *types.AddOrderRequest) (resp *types.Response, err error) {
	add, err := l.svcCtx.GoodsSrv.AddOrder(l.ctx, &goodssrv.AddOrderRequest{
		UserId:  req.UserId,
		GoodsId: req.GoodsId,
		Num:     req.Num,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	return &types.Response{
		Code: 200,
		Msg:  "生成订单成功",
		Data: map[string]interface{}{
			"OrderId": add.OrderId,
			"PayUrl":  add.PayUrl,
		},
	}, nil
}
